package com.bootdo.wx.service.impl;

import com.bootdo.wx.dao.MemberAddressDao;
import com.bootdo.wx.dao.OrderShippingDao;
import com.bootdo.wx.domain.MemberAddressDO;
import com.bootdo.wx.domain.OrderShippingDO;
import com.bootdo.wx.service.MemberAddressService;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class MemberAddressServiceImpl implements MemberAddressService {
	@Autowired
	private MemberAddressDao memberAddressDao;

	@Autowired
	private OrderShippingDao orderShippingDao;

	@Override
	public MemberAddressDO get(String id){
		return memberAddressDao.get(id);
	}
	
	@Override
	public List<MemberAddressDO> list(Map<String, Object> map){
		return memberAddressDao.list(map);
	}

	@Override
	public List<MemberAddressDO> queryByUserId(Map<String, Object> map) {
		return memberAddressDao.queryByUserId(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return memberAddressDao.count(map);
	}
	
	@Override
	public int save(MemberAddressDO memberAddress){
		return memberAddressDao.save(memberAddress);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public int add(Map<String, Object> map) {
		String userId = map.get("userId").toString();
		String province = map.get("province").toString();
		String city = map.get("city").toString();
		String county = map.get("county").toString();
		String address = map.get("address").toString();
		String fullName = map.get("fullName").toString();
		String phone = map.get("phone").toString();
		int defaul = Integer.parseInt(map.get("default").toString());

		MemberAddressDO addressDO = new MemberAddressDO();
		addressDO.setId(UuidUtil.get32UUID());
		addressDO.setAddress(address);
		addressDO.setCity(city);
		addressDO.setFullName(fullName);
		addressDO.setProvince(province);
		addressDO.setCounty(county);
		addressDO.setPhone(phone);
		addressDO.setDefaultValue(defaul);
		addressDO.setDelFlag(0);
		addressDO.setOpAt(new Date());
		addressDO.setUserId(userId);
		if (defaul==0){
			memberAddressDao.removeDefault(userId);
		} else {
			Map<String,Object> map2 = new HashMap<String,Object>();
			map2.put("userId",userId);
			map2.put("delFlag","0");
			map2.put("defaultValue","0");
			int size = memberAddressDao.list(map2).size();
			if (size == 0) {
				addressDO.setDefaultValue(0);
			}
		}
		return memberAddressDao.save(addressDO);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public int change(Map<String, Object> map) {
		String addressId = map.get("addressId").toString();
		String userId = map.get("userId").toString();
		String province = map.get("province").toString();
		String city = map.get("city").toString();
		String county = map.get("county").toString();
		String address = map.get("address").toString();
		String fullName = map.get("fullName").toString();
		String phone = map.get("phone").toString();
		int defaul = Integer.parseInt(map.get("default").toString());

		MemberAddressDO addressDO = memberAddressDao.get(addressId);
		addressDO.setAddress(address);
		addressDO.setCity(city);
		addressDO.setFullName(fullName);
		addressDO.setProvince(province);
		addressDO.setCounty(county);
		addressDO.setPhone(phone);
		addressDO.setDefaultValue(defaul);
		addressDO.setOpAt(new Date());
		addressDO.setUserId(userId);
		if (defaul==0){
			memberAddressDao.removeDefault(userId);
		}
		return memberAddressDao.update(addressDO);
	}

	//查看订单地址
	@Override
	public Map<String, Object> selectAddress(Map<String, Object> map) {
		Map<String, Object> returnMap =new HashMap<>();
		OrderShippingDO orderShipping = orderShippingDao.getByOrderId(map.get("orderId").toString());
		returnMap.put("orderShipping",orderShipping);
		return returnMap;
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R toDefault(Map<String,Object> map) {
		String addressId = map.get("addressId").toString();
		String userId = map.get("userId").toString();
		MemberAddressDO addressDO = memberAddressDao.get(addressId);
		if (!userId.equals(addressDO.getUserId())){
			return R.error("收货地址异常");
		}
		memberAddressDao.removeDefault(userId);

		addressDO.setDefaultValue(0);
		memberAddressDao.update(addressDO);
		return R.ok();
	}

	@Override
	public int update(MemberAddressDO memberAddress){
		return memberAddressDao.update(memberAddress);
	}
	
	@Override
	public int remove(String id){
		return memberAddressDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberAddressDao.batchRemove(ids);
	}
	
}
